import {
  postUsersListData,
  deleteUserById,
  newUserData,
  editUserData
} from '@/service/main/user/user'
import { defineStore } from 'pinia'
import type { ISUserState } from './type'

const useUserStore = defineStore('user', {
  state: (): ISUserState => ({
    usersList: [],
    usersTotalCount: 0
  }),
  actions: {
    async postUsersListAction(queryInfo: any) {
      const usersListResult = await postUsersListData({
        status: 0,
        ...queryInfo
      })
      const { totalCount, list } = usersListResult.data
      this.usersTotalCount = totalCount
      this.usersList = list
    },

    async deleteUserByIdAction(id: number) {
      // 1.删除数据操作
      const deleteResult = await deleteUserById(id)
      // 2.重新请求新的数据
      this.postUsersListAction({ offset: 0, size: 10 })
      return deleteResult
    },

    async newUserDataAction(userInfo: any) {
      // 1.创建新的用户
      const newResult = await newUserData(userInfo)
      console.log(newResult)

      // 2.重新请求新的数据
      this.postUsersListAction({ offset: 0, size: 10 })
    },

    async editUserDataAction(id: number, userInfo: any) {
      // 1.更新用户的数据
      const editResult = await editUserData(id, userInfo)

      // 2.重新请求新的数据
      this.postUsersListAction({ offset: 0, size: 10 })
    }
  }
})

export default useUserStore
